package com.gmadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmadmin.common.ApiResponse;
import com.gmadmin.entity.dto.SysUserDto;
import com.gmadmin.entity.SysUser;
import com.gmadmin.payload.UserQueryCriteria;
import com.gmadmin.service.SysUserService;
import com.gmadmin.entity.vo.user.SysUserVo;
import com.gmadmin.entity.vo.user.UserPrincipal;
import com.gmadmin.utils.SecurityUtils;
import com.gmadmin.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLang
 * @Date: 2021/12/30/22:12
 * @Description: 用户管理
 */
@Api(tags = "系统:用户管理")
@RequestMapping("/api/system/user")
@RestController()
public class SysUserController {

    private final SysUserService userService;

    public SysUserController(SysUserService userService) {
        this.userService = userService;
    }


    @ApiOperation(value = "获取当前登录用户详情")
    @GetMapping("/info")
    public ApiResponse<UserPrincipal> userInfo() {
        return ApiResponse.ofSuccess((UserPrincipal) SecurityUtils.getCurrentUser());
    }


    @ApiOperation(value = "分页获取用户列表")
    @GetMapping("page")
    public ApiResponse<PageVo<SysUserVo>> findUserByPage(UserQueryCriteria criteria, Page<SysUser> page) {
        PageVo<SysUserVo> pageVo = (PageVo<SysUserVo>) userService.findListByPage(criteria, page);
        return ApiResponse.ofSuccess(pageVo);
    }

    /**
     * 新增用户
     * @param userDto 用户信息接收类
     */
    @ApiOperation(value = "新增用户")
    @PostMapping
    public ApiResponse saveOrUpdateUser(@RequestBody SysUserDto userDto){
        userService.saveOrUpdateUser(userDto);
        return ApiResponse.ofSuccess();
    }


    @ApiOperation(value = "删除用户,逻辑删除")
    @DeleteMapping
    public ApiResponse deleteUser(@RequestParam("id") Long id){
        userService.delUser(id);
        return ApiResponse.ofSuccess();
    }



}
